﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml;
using SK3000RunningTestTool.SKService;
using SK3000RunningTestTool.SKServerDelegate;
using SK3000RunningTestTool.SKServerEnum;
using SK3000RunningTestTool.SKServerSource;

namespace SK3000RunningTestTool
{
    public partial class FrmMain : DevComponents.DotNetBar.Metro.MetroForm
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            Init();
        }

        public void Init()
        {
            ServiceTestSource.Init();
            Init_ControlInit();
        }
        public void Init_ControlInit()
        {
            panel1.Controls.Clear();
            for (int i = ServiceTestSource.ServiceTestList.Count - 1; i >= 0; i--)
            {
                ISKService item = ServiceTestSource.ServiceTestList[i];
                item.ServiceTestCallbackEvent += Result_ServiceTestCallbackEvent;
                SetControl(item);
            }
        }

        private void Result_ServiceTestCallbackEvent(object sender, ServiceTestCallbackValue ServiceTestCallbackValue)
        {
            ISKService test = (ISKService)sender;
            string strPrompt = test.ServiceName;
            switch (ServiceTestCallbackValue.EventValue)
            {
                case Enum_ServiceTestEvent.ServiceTestStart:
                    strPrompt += "[服务测试开始]";
                    break;
                case Enum_ServiceTestEvent.ServiceTestEnd:
                    strPrompt += "[服务测试结束]";
                    break;
                case Enum_ServiceTestEvent.ConnectServiceing:
                    strPrompt += "[连接服务中]";
                    break;
                case Enum_ServiceTestEvent.ConnectSuccess:
                    strPrompt += "[服务连接成功]";
                    break;
                case Enum_ServiceTestEvent.ConnectException:
                    strPrompt += "[服务连接失败]";
                    break;
                case Enum_ServiceTestEvent.ConnectServiced:
                    strPrompt += "[服务连接完成]";
                    break;
                case Enum_ServiceTestEvent.SendTestDataing:
                    strPrompt += "[发送测试数据]"+ ServiceTestCallbackValue.EventMessage;
                    break;

                case Enum_ServiceTestEvent.SendTestDataSuccess:
                    strPrompt += "[收到返回数据]"+ ServiceTestCallbackValue.EventMessage;
                    break;

                case Enum_ServiceTestEvent.SendTestDataException:
                    strPrompt += "[测试数据失败]";
                    break;
                case Enum_ServiceTestEvent.ConnectCloseing:
                    strPrompt += "[关闭服务连接]";
                    break;
                case Enum_ServiceTestEvent.ConnectClosed:
                    strPrompt += "[服务连接完成]";
                    break;
            }
            this.BeginInvoke(new EventHandler(delegate
            {
                Test(strPrompt);
            }));
            Console.WriteLine(DateTime.Now.ToString() + "执行测试结束，结果:" + ServiceTestCallbackValue.EventValue.ToString());
        }

        public void SetControl(ISKService serviceTest)
        {
            CustomControl.ServiceTestStatusControl cServiceTest = new CustomControl.ServiceTestStatusControl();
            cServiceTest.BackColor = System.Drawing.Color.White;
            cServiceTest.Location = new System.Drawing.Point(482, 25);
            cServiceTest.Name = "cServiceTest" + serviceTest.ServiceName;
            cServiceTest.Size = new System.Drawing.Size(261, 80);
            cServiceTest.Dock = DockStyle.Top;
            cServiceTest.SetService(serviceTest);
            panel1.Controls.Add(cServiceTest);
        }




        private void Test_ServiceTestCallbackEvent(object sender, ServiceTestCallbackValue ServiceTestCallbackValue)
        {
            ISKService test = (ISKService)sender;
            this.BeginInvoke(new EventHandler(delegate
            {
                GroupBox g = (GroupBox)test.Tag;
                foreach (Control c in g.Controls)
                {
                    if (c.Name.Contains("lblPrompt"))
                    {
                        Label lbl = (Label)c;
                        lbl.Text = ServiceTestCallbackValue.EventValue.ToString();
                    }
                }
            }));
            Console.WriteLine(DateTime.Now.ToString() + "执行测试结束，结果:" + ServiceTestCallbackValue.EventValue.ToString());
        }

        private void btnTestAll_Click(object sender, EventArgs e)
        {
            foreach (Control c in panel1.Controls)
            {
                if(c.GetType()==typeof(CustomControl.ServiceTestStatusControl))
                {
                    ISKService testService = (ISKService)c;
                    testService.ServiceTestAsync(null);
                }
            }
        }

        public bool Test(string s)
        {
            bool bolResult = false;
            lstMsg.Items.Add(DateTime.Now.ToString() + "_" + s);
            return bolResult;
        }

        private void btnLogClear_Click(object sender, EventArgs e)
        {
            lstMsg.Items.Clear();
        }
        
        private void btnServiceTestSet_Click(object sender, EventArgs e)
        {
            FrmSystemSetting frm = new FrmSystemSetting();
            frm.ShowDialog();
            Init_ControlInit();
        }
    }
}
